'use strict'

const { questionnaire, questionnaireAnswers, QSSelect, personalDetails } = require('../models');
const dateTime = require('../dateTime');
const auth = require('../utils/auth');

// 调查问卷页面
var fn_QS1 = async (ctx, next) => {
  //用户名
  var username = ctx.session.userName;

  // var url = ctx.url;
  // console.log(url);
  // console.log(ctx.query.id);

  // 获取当前路由id=？的值
  var QSId = ctx.query.id;

  //用户id
  // var userId = ctx.session.userId;
  //通过当前用户点击前往的问卷Id 查找 用户点击前往的问卷
  //获取当前用户Id所点击的问卷Id
  // var userClickQs = await QSSelect.findAll({
  //   order: [
  //     ['id', 'desc']
  //   ],
  //   where: {
  //     userId: userId
  //   }
  // });
  // // console.log(userClickQs);
  // var questionnaireId = userClickQs[0].questionnaireId;

  // 获取与点击前往的问卷id相等的问卷
  var userClickQs2 = await questionnaire.findOne({
    where: {
      id: QSId
    }
  });

  // console.log(userClickQs2);

  var QSTheme = userClickQs2.qsName;
  //问卷编号
  var qsId = userClickQs2.id;
  //问卷发布人
  var publishUserId = userClickQs2.publishUserId;
  //问卷名
  var qsName = userClickQs2.qsName;
  //问卷简介
  var qsSynopsis = userClickQs2.qsSynopsis;
  //问卷题目

  var topicOne = userClickQs2.topicOne;
  var topicTwo = userClickQs2.topicTwo;
  var topicThree = userClickQs2.topicThree;
  var topicFour = userClickQs2.topicFour;
  var topicFive = userClickQs2.topicFive;
  var topicSix = userClickQs2.topicSix;
  var topicSeven = userClickQs2.topicSeven;
  var topicEight = userClickQs2.topicEight;
  var topicNine = userClickQs2.topicNine;
  var topicTen = userClickQs2.topicTen;

  //问卷创建时间
  // var createdAt = userClickQs2.createdAt;
  // console.log(qsId);
  // console.log(publishUserId);
  // console.log(qsName);
  // console.log(qsSynopsis);
  // console.log(topicFive);

  // 获取创建时间并且转换时间
  var createdAt = dateTime(userClickQs2.createdAt);
  // console.log(createdAt2);

  var obj = {
    //问卷编号
    qsId,
    //问卷主题
    QSTheme,
    //问卷发布人
    publishUserId,
    //问卷名
    qsName,
    //问卷简介
    qsSynopsis,
    //问卷题目
    topicOne,
    topicTwo,
    topicThree,
    topicFour,
    topicFive,
    topicSix,
    topicSeven,
    topicEight,
    topicNine,
    topicTen,
    //用户名称
    username,
    //问卷创建时间
    createdAt
  }
  ctx.render("QS1.html", obj)

};

//用户点击的是哪个表单判断
var fn_QSJudge = async (ctx, next) => {

  //获取用户点击前往的问卷id
  var qsId = ctx.request.body.qoeId.trim();

  //获取点击用户Id
  // var userId = ctx.session.userId;

  //将点击前往的问卷id与点击用户Id存入数据库
  // var QSSelects = await QSSelect.create({
  //   questionnaireId: qsId,
  //   userId: userId
  // });

  //  获取点击问卷的id传入前端
  var url = '/SQ1/?id=' + qsId; //SQ1/?id=qsId
  // console.log(url);

  let res = "";
  res = { code: 200, url: url };
  ctx.body = JSON.stringify(res);
};
//

// 问卷一表单提交
var fn_QS1Judge = async (ctx, next) => {

  //问卷一答题人Id
  var questionUserId = ctx.session.userId;

  //问卷一答题人答案
  var question1 = ctx.request.body.Question1;
  var question2 = ctx.request.body.Question2;
  var question3 = ctx.request.body.Question3;
  var question4 = ctx.request.body.Question4;
  var question5 = ctx.request.body.Question5;
  var question6 = ctx.request.body.Question6;
  var question7 = ctx.request.body.Question7;
  var question8 = ctx.request.body.Question8;
  var question9 = ctx.request.body.Question9;
  var question10 = ctx.request.body.Question10;

  //问卷Id
  var qsId = ctx.request.body.qsId;

  // console.log(question1);
  // console.log(questionUserId);
  // console.log(qsId);

  // 查询用户是否重复填写，是的话则覆盖之前填写的
  var questionnaireAnswerss = await questionnaireAnswers.findOne({
    where: {
      answerUserId: questionUserId,
      questionnaireId: qsId
    }
  });

  if (questionnaireAnswerss === null) {
    //如果没有该用户相对的问卷则创建

    // 向questionnaireAnswers答案表存答案
    var questionnaireAnswerss = await questionnaireAnswers.create({
      questionnaireId: qsId,
      answerUserId: questionUserId,
      topicOneAnswer: question1,
      topicTwoAnswer: question2,
      topicThreeAnswer: question3,
      topicFourAnswer: question4,
      topicFiveAnswer: question5,
      topicSixAnswer: question6,
      topicSevenAnswer: question7,
      topicEightAnswer: question8,
      topicNineAnswer: question9,
      topicTenAnswer: question10,
    });

  } else {
    //如果有该用户则更新
    await questionnaireAnswers.update({
      topicOneAnswer: question1,
      topicTwoAnswer: question2,
      topicThreeAnswer: question3,
      topicFourAnswer: question4,
      topicFiveAnswer: question5,
      topicSixAnswer: question6,
      topicSevenAnswer: question7,
      topicEightAnswer: question8,
      topicNineAnswer: question9,
      topicTenAnswer: question10,
    }, {
      where: {
        answerUserId: questionUserId,
        questionnaireId: qsId,
      }

    });
  }




  let res = "";
  res = { code: 200, msg: "参与调查问卷成功" };
  ctx.body = JSON.stringify(res);
};

// 问卷提交成功页面
var fn_QS1Ok = async (ctx, next) => {
  //提交成功
  var obj = {
    // 网页头像
    icon: "/statics/img/QSOkicon.png",
    // 网页插图
    inbetweening: "/statics/img/QSOk.png",
    // 网页标题
    title: "提交成功",
    // 错误内容
    OkContent: "谢谢你参与，我们将会妥善保管此调查问卷",
    // 按钮一路径
    router1: '/home/SQ1',
    // 按钮一文本
    button1: "再填写一次",
    // 按钮二路径
    router2: '/home',
    // 按钮二文本
    button2: "参与其它问卷"
  }
  ctx.render('QSOk.html', obj)
};

// 已发布问卷删除按钮
var fn_userQsDelete = async (ctx, next) => {
  var userQsIdDelete = ctx.request.body.qsId;
  // console.log(userQsIdDelete);

  await questionnaire.update({
    state: '2'
  }, {
    where: {
      id: userQsIdDelete
    }
  });

  let res = "";
  res = { code: 200, msg: "删除成功" };
  ctx.body = JSON.stringify(res);
};

module.exports = {
  '/SQ1/': ['get', auth, fn_QS1],
  '/home/SQ1Ok': ['get', fn_QS1Ok],
  '/home/SQJudge': ['post', fn_QSJudge],
  '/home/SQ1judge': ['post', fn_QS1Judge],
  '/home/userQsDelete': ['post', fn_userQsDelete],
}